package cn.iocoder.yudao.module.mybusiness.util;


import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.websocket.core.sender.AbstractWebSocketMessageSender;
import cn.iocoder.yudao.framework.websocket.core.sender.WebSocketMessageSender;
import cn.iocoder.yudao.module.infra.api.websocket.WebSocketSenderApi;
import cn.iocoder.yudao.module.mybusiness.domain.ProductionRecord;
import cn.iocoder.yudao.module.mybusiness.domain.Task;
import cn.iocoder.yudao.module.mybusiness.mapper.FacilityMapper;
import cn.iocoder.yudao.module.mybusiness.mapper.RecordMapper;
import cn.iocoder.yudao.module.mybusiness.mapper.TaskMapper;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import javafx.util.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@EnableScheduling//开启定时任务
@Component
public class SpringBootTask {

    private static DateTimeFormatter pattern=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Resource
    private WebSocketSenderApi webSocketSenderApi;

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private RecordMapper recordMapper;

    @Autowired
    private FacilityMapper facilityMapper;

    @Autowired
    private InitCraft initCraft;

    @Scheduled(cron="0/10 * * * * ?")//每10s 更新任务状态
    public void upTaskStatus(){
        LocalDateTime now=LocalDateTime.now();
        List<Task> taskList = taskMapper.selectTaskComplete();
        taskMapper.upTaskStatus(pattern.format(now),2);
        taskList.forEach(task -> {
            JSONObject jsonObject = new JSONObject();
            String text = task.getName()+"已完成";
            jsonObject.put("text",text);
            webSocketSenderApi.send(2,"task_result",jsonObject.toJSONString());
        });
    }

    @Scheduled(cron="0 * * * * ?")//每分钟 参数分析
    public void taskCity(){
        LocalDateTime now=LocalDateTime.now();
        System.out.println("taskCraft 这是定时任务，时间是："+pattern.format(now));
        List<String> crafts = facilityMapper.selectCraft();
        initCraft.getCraftList().clear();
        initCraft.getStandardChanceList().clear();
        crafts.forEach(craft->{
            List<ProductionRecord> records = recordMapper.selectList(new LambdaQueryWrapperX<ProductionRecord>().eq(ProductionRecord::getCraft,craft));
            if(!CollectionUtils.isEmpty(records)){
                MaxEnt maxEnt = new MaxEnt();
                maxEnt.loadData(records,"result");
                maxEnt.train(10);
                List<String> fieldList = new ArrayList<String>();
                fieldList.add(craft); //毕业生
                Pair<String, Double>[] result = maxEnt.predict(fieldList);
                System.out.println(craft+"====="+JSONObject.toJSONString(result));
                Iterator<Pair<String, Double>> iterator = Arrays.stream(result).iterator();
                initCraft.getCraftList().add(craft);
                while (iterator.hasNext()){
                    Pair<String, Double> next = iterator.next();
                    String key = next.getKey();
                    Double value = next.getValue();
                    DecimalFormat df = new DecimalFormat("#.##");
                    String formattedValue = df.format(value);
                    double v = Double.valueOf(formattedValue) * 100;
                    if("1".equals(key)){
                        initCraft.getStandardChanceList().add(v);
                    }
                }
            }
        });
    }

}
